package com.pengcheng.hngateway.channel;

import ch.qos.logback.core.util.TimeUtil;
import com.pengcheng.hngateway.adapter.BootNettyChannelInboundHandlerAdapter;
import com.pengcheng.hngateway.util.Common;
import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import io.netty.handler.codec.bytes.ByteArrayDecoder;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import io.netty.handler.timeout.IdleStateHandler;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;

public class BootNettyChannelInitializer<SocketChannel>  extends ChannelInitializer<Channel> {
    private Common common;
    private RabbitTemplate rabbitTemplate;

    public BootNettyChannelInitializer(Common common, RabbitTemplate rabbitTemplate) {
        this.common = common;
        this.rabbitTemplate = rabbitTemplate;
    }

    @Override
    protected void initChannel(Channel channel) throws Exception {
        //添加编解码，此处代码为解析tcp传过来的参数，为UTF-8格式，可以自定义解码格式
        // ChannelOutboundHandler，依照逆序执行
//        channel.pipeline().addLast("encoder", new ByteArrayDecoder());

        // 属于ChannelInboundHandler，依照顺序执行
        channel.pipeline().addLast("decoder", new ByteArrayDecoder());
        channel.pipeline().addLast(new IdleStateHandler(30,0,0));
        /**
         * 自定义ChannelInboundHandlerAdapter
         */
        channel.pipeline().addLast(new BootNettyChannelInboundHandlerAdapter(common,rabbitTemplate));
    }
}
